package com.javabasic.topic;

import java.util.Random;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2021/12/22 11:57 下午
 */
public class SortAlgorithm {

    static final int ELEMENT_BOUND_SEED = 100;
    static Random random = new Random(ELEMENT_BOUND_SEED);
    static final int ARRAY_SIZE = 10;

    public static void main(String[] args) {
        int[] arr = new int[ARRAY_SIZE];

        {
            for (int i = 0; i < ARRAY_SIZE; i++) {
                arr[i] = random.nextInt(ELEMENT_BOUND_SEED);
            }

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }

            System.out.println();

            bubbleSort(arr, 0, ARRAY_SIZE - 1);

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }
        }

        System.out.println("\n================================");


        {
            for (int i = 0; i < ARRAY_SIZE; i++) {
                arr[i] = random.nextInt(ELEMENT_BOUND_SEED);
            }

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }

            System.out.println();

            selectSort(arr, 0, ARRAY_SIZE - 1);

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }
        }

        System.out.println("\n================================");


        {
            for (int i = 0; i < ARRAY_SIZE; i++) {
                arr[i] = random.nextInt(ELEMENT_BOUND_SEED);
            }

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }

            System.out.println();

            insertSort(arr, 0, ARRAY_SIZE - 1);

            for (int i = 0; i < ARRAY_SIZE; i++) {
                System.out.print(arr[i] + " ");
            }
        }


    }

    /**
     * 冒泡排序
     * @param arr
     * @param left
     * @param right
     */
    private static void bubbleSort(int[] arr, int left, int right) {
        int range = right - left;
        for (int i = 0; i < range; i++) {
            for (int j = left + 1; j <= right - i; j++) {
                if (arr[j - 1] > arr[j]) {
                    int temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    /**
     * 选择排序
     * @param arr
     * @param left
     * @param right
     */
    private static void selectSort(int[] arr, int left, int right) {
        for (int i = left; i < right; i++) {
            int tagIndex = i;
            for (int j = i + 1; j <= right; j++) {
                if (arr[j] < arr[tagIndex]) {
                    tagIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[tagIndex];
            arr[tagIndex] = temp;
        }
    }


    /**
     * 插入排序
     * @param arr
     * @param left
     * @param right
     */
    private static void insertSort(int[] arr, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            for (int j = i - 1; j >= left; j--) {
                if (arr[j] <= arr[j + 1]) {
                    break;
                }
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }


}
